package com.crazyauntzhang.easyshopbackend.controller;

import com.crazyauntzhang.easyshopbackend.domain.SupplementFlow;
import com.crazyauntzhang.easyshopbackend.domain.Supplier;
import com.crazyauntzhang.easyshopbackend.request.AddSupplierResquest;
import com.crazyauntzhang.easyshopbackend.request.DeleteSupplierRequest;
import com.crazyauntzhang.easyshopbackend.request.EditSupplierRequest;
import com.crazyauntzhang.easyshopbackend.request.FindSupplierRequest;
import com.crazyauntzhang.easyshopbackend.result.ResponseResult;
import com.crazyauntzhang.easyshopbackend.service.SupplementFlowService;
import com.crazyauntzhang.easyshopbackend.service.SupplierService;
import com.crazyauntzhang.easyshopbackend.util.FormatChecker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class SupplierController {

	private final SupplierService supplierService;
	private final SupplementFlowService supplementFlowService;

	@Autowired
	public SupplierController(SupplierService supplierService,
							  SupplementFlowService supplementFlowService) {
		this.supplierService = supplierService;
		this.supplementFlowService = supplementFlowService;
	}

	/**
	 * 查询所有供应商
	 *
	 * @return 供应商列表请求体
	 */
	@PostMapping("/findAllSuppliers")
	public ResponseResult<Object> findSuppliers() {
		List<Supplier> supplierList = supplierService.findAllSuppliers();
		if (supplierList.isEmpty())
			return new ResponseResult<>(10404, "Supplier Not Found");
		return new ResponseResult<>(10200, "OK", supplierList);
	}

	/**
	 * 按编号或名称查询供应商
	 *
	 * @param findSupplierRequest 请求体
	 * @return 响应体
	 */
	@Deprecated
	@PostMapping("/findSupplier")
	public ResponseResult<Object> findSupplier(@RequestBody FindSupplierRequest findSupplierRequest) {
		String supplierIdOrName = findSupplierRequest.getSupplierIdOrName();

		List<Supplier> supplierList = new ArrayList<>();

		if (FormatChecker.isNumber(supplierIdOrName)) {
			Supplier supplier = supplierService.findSupplierById(supplierIdOrName);
			if (supplier == null)
				return new ResponseResult<>(10404, "Supplier Not Found");
			supplierList.add(supplier);
		} else {
			supplierList = supplierService.findSupplierByName(supplierIdOrName);
			if (supplierList.isEmpty())
				return new ResponseResult<>(10404, "Supplier Not Found");
		}
		return new ResponseResult<>(10200, "OK", supplierList);
	}

	/**
	 * 按编号查询供应商
	 *
	 * @param findSupplierRequest 请求体
	 * @return 响应体
	 */
	@PostMapping("/findSupplierById")
	public ResponseResult<Object> findSupplierById(@RequestBody FindSupplierRequest findSupplierRequest) {
		String supplierID = findSupplierRequest.getSupplierID();
		Supplier supplier = supplierService.findSupplierById(supplierID);
		if (supplier == null)
			return new ResponseResult<>(10404, "Supplier Not Found");
		List<Supplier> supplierList = new ArrayList<>();
		supplierList.add(supplier);
		return new ResponseResult<>(10200, "OK", supplierList);
	}

	/**
	 * 按名称查询供应商
	 *
	 * @param findSupplierRequest 请求体
	 * @return 响应体
	 */
	@PostMapping("/findSupplierByName")
	public ResponseResult<Object> findSupplierByName(@RequestBody FindSupplierRequest findSupplierRequest) {
		String supplierName = findSupplierRequest.getSupplierName();
		List<Supplier> supplierList = supplierService.findSupplierByName(supplierName);
		if (supplierList.isEmpty())
			return new ResponseResult<>(10404, "Supplier Not Found");
		return new ResponseResult<>(10200, "OK", supplierList);
	}

	/**
	 * 添加供应商
	 *
	 * @param addSupplierResquest 请求体
	 * @return 响应体
	 */
	@PostMapping("/addSupplier")
	public ResponseResult<Object> addSupplier(@RequestBody AddSupplierResquest addSupplierResquest) {
		String supplierID = addSupplierResquest.getSupplierID();
		if (supplierService.findSupplierById(supplierID) != null)
			return new ResponseResult<>(10412, "Supplier Exists");
		String supplierName = addSupplierResquest.getSupplierName();
		String address = addSupplierResquest.getAddress();
		String tel = addSupplierResquest.getTel();

		supplierService.addSupplier(supplierID, supplierName, address, tel);

		Supplier supplier = new Supplier(supplierID, supplierName, address, tel);
		return new ResponseResult<>(10200, "OK", supplier);
	}

	/**
	 * 修改供应商
	 *
	 * @param editSupplierRequest 请求体
	 * @return 响应体
	 */
	@PostMapping("/editSupplier")
	public ResponseResult<Object> editSupplier(@RequestBody EditSupplierRequest editSupplierRequest) {
		String supplierID = editSupplierRequest.getSupplierID();
		String supplierName = editSupplierRequest.getSupplierName();
		String address = editSupplierRequest.getAddress();
		String tel = editSupplierRequest.getTel();

		Supplier supplier = supplierService.findSupplierById(supplierID);
		if (supplier == null)
			return new ResponseResult<>(10404, "Supplier Not Found");

		if (supplierName == null || supplierName.isBlank())
			supplierName = supplier.getSupplierName();
		if (address == null || address.isBlank())
			address = supplier.getAddress();
		if (tel == null || tel.isBlank()) tel = supplier.getTel();

		supplierService.changeSupplier(supplierID, supplierID, supplierName, address, tel);

		Supplier editedSupplier = new Supplier(supplierID, supplierName, address, tel);
		return new ResponseResult<>(10200, "OK", editedSupplier);
	}

	/**
	 * 删除供应商
	 *
	 * @param deleteSupplierRequest 请求体
	 * @return 响应体
	 */
	@PostMapping("/deleteSupplier")
	public ResponseResult<Object> deleteSupplier(@RequestBody DeleteSupplierRequest deleteSupplierRequest) {
		String supplierID = deleteSupplierRequest.getSupplierID();

		Supplier supplier = supplierService.findSupplierById(supplierID);
		if (supplier == null)
			return new ResponseResult<>(10404, "Supplier Not Found");

		List<SupplementFlow> supplementFlowList = supplementFlowService.findFlowBySupplierID(supplierID);
		if (supplementFlowList.isEmpty())
			supplierService.deleteSupplier(supplierID);
		else
			return new ResponseResult<>(10412, "Supplier Works");

		return new ResponseResult<>(10200, "OK");
	}

}
